package com.vincce.fitness.core.util;

import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: long
 * Date: 10/31/13
 * Time: 11:02 AM
 * To change this template use File | Settings | File Templates.
 */
public class CommonUtil {

	/**
	 * 处理上行手机
	 * @param mobile
	 * @return
	 */
	public static boolean checkMobile(String mobile) {
		String str= "^((13[0-9])|(15[0-9])|(18[0-9])|(14[0-9]))\\d{8}$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(mobile);
		return m.matches();
	}





    /**
     * 15位身份证号码的基本数字和位数验校
     *
     * @param idcard
     * @return
     */
    public static boolean is15Idcard(String idcard) {
        return idcard == null || "".equals(idcard) ? false : Pattern.matches(
                "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$",
                idcard);
    }

    /**
     * 18位身份证号码的基本数字和位数验校
     *
     * @param idcard
     * @return
     */
    public static boolean is18Idcard(String idcard) {
        return Pattern
                .matches(
                        "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$",
                        idcard);
    }


    /**
     * 通过身份证号获取性别
     * @return String
     */
    public static String getSexFromId(String tIdNo)
    {
        if (tIdNo.length() != 15 && tIdNo.length() != 18)
        {
            return "";
        }
        String sex = "";
        if (tIdNo.length() == 15)
        {
            sex = tIdNo.substring(14, 15);
        }
        else
        {
            sex = tIdNo.substring(16, 17);
        }
        try
        {
            int iSex = Integer.parseInt(sex);
            iSex %= 2;
            if (iSex == 0)
            {
                return "1";
            }
            if (iSex == 1)
            {
                return "0";
            }
        }
        catch (Exception ex)
        {
            return "";
        }
        return "";
    }


    /**
     * 通过身份证号号获取生日日期
     * @return String
     */
    public static String getBirthdayFromId(String tIdNo)
    {
        String birthday = "";
        if (tIdNo.length() != 15 && tIdNo.length() != 18)
        {
            return "";
        }
        if (tIdNo.length() == 18)
        {
            birthday = tIdNo.substring(6, 14);
            birthday = birthday.substring(0, 4) + "-" + birthday.substring(4, 6) + "-" + birthday.substring(6);
        }
        if (tIdNo.length() == 15)
        {
            birthday = tIdNo.substring(6, 12);
            birthday = birthday.substring(0, 2) + "-" + birthday.substring(2, 4) + "-" + birthday.substring(4);
            birthday = "19" + birthday;
        }
        return birthday;

    }


    /**
     * 获取时间差
     * @param num
     * @return
     */
    public static Date getDelta(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, num);
        return calendar.getTime();
    }


    /**
     * 获取18位身份证号码
     * @param
     * @return
     */
        public static String getEighteenIDCard(String fifteenIDCard) throws Exception{
            StringBuilder sb;
            if(fifteenIDCard != null && fifteenIDCard.length() == 15){
                sb = new StringBuilder();
                sb.append(fifteenIDCard.substring(0, 6))
                        .append("19")
                        .append(fifteenIDCard.substring(6));
                sb.append(getVerifyCode(sb.toString()));
                return sb.toString();
            } else {
                throw new Exception("不是15位的身份证");    }    }

        public static char getVerifyCode(String idCardNumber) throws Exception{
            if(idCardNumber == null || idCardNumber.length() < 17) {
                throw new Exception("不合法的身份证号码");
            }
            char[] Ai = idCardNumber.toCharArray();
            int[] Wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
            char[] verifyCode = {'1','0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
            int S = 0;
            int Y;
            for(int i = 0; i < Wi.length; i++){
                S += (Ai[i] - '0') * Wi[i];
            }
            Y = S % 11;
            return verifyCode[Y];
        }

        public static  boolean verify(String idCardNumber) throws Exception{
            if(idCardNumber == null || idCardNumber.length() != 18) {
                throw new Exception("不是18位的身份证号码");
            }
            return getVerifyCode(idCardNumber) == idCardNumber.charAt(idCardNumber.length() - 1);
        }


    public static int getAge(String IDCardNum){
        Calendar cal1 = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
        //System.out.println(IDCardNum.substring(12,14));
        cal1.set(Integer.parseInt(IDCardNum.substring(6,10)),  //年
                Integer.parseInt(IDCardNum.substring(10,12)),  //月
                Integer.parseInt(IDCardNum.substring(12,14)));  //日
        return getYearDiff(today, cal1);
    }

    static int getYearDiff(Calendar cal, Calendar cal1){
        int m = (cal.get(cal.MONTH)) - (cal1.get(cal1.MONTH));
        int y = (cal.get(cal.YEAR)) - (cal1.get(cal1.YEAR));
        return (y * 12 + m)/12;
    }


	public static Date getMonthsDate(Date date,int months) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH,months);
		c.set(Calendar.DAY_OF_MONTH, 1);
		date = c.getTime();
		return date;
	}

	/**
	 * 得到当前月的最后一天
	 *
	 * @return String
	 */
	public static Date getLastDayOfMonth(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, 1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.DAY_OF_MONTH, -1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.clear(Calendar.MINUTE);
		c.clear(Calendar.SECOND);
		c.clear(Calendar.MILLISECOND);
		date = c.getTime();
		return date;
	}

	/**
	 * 得到当前月的第一天
	 *
	 * @return String
	 */
	public static Date getFirstDayOfMonth(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.clear(Calendar.MINUTE);
		c.clear(Calendar.SECOND);
		c.clear(Calendar.MILLISECOND);
		date = c.getTime();
		return date;
	}

}
